/*
 * @Descripttion: 定列处理函数
 * @version: v1.0
 * @Author: 34786
 * @Date: 2020-08-12 08:58:16
 * @LastEditors: 34786
 * @LastEditTime: 2021-07-08 14:55:10
 */

const allEditTypes = {
  input: "ElInput",
  select: "ElSelect",
  datePicker: "ElDatePicker",
  popSelect: "PopSelect",
  ElInput: "ElInput",
  ElSelect: "ElSelect",
  ElDatePicker: "ElDatePicker",
  ElInputNumber: "ElInputNumber",
  ElTimeSelect: "ElTimeSelect",
  ElCascader: "ElCascader",
  ElSwitch: "ElSwitch",
  ElRate: "ElRate",
  PopSelect: "PopSelect",
  PriceInput: "PriceInput",
  RateInput: "RateInput",
  UnitInput: "UnitInput"
};
export const editTypeTransform = function(type) {
  let editOpt = {};
  if (allEditTypes[type]) {
    editOpt["name"] = allEditTypes[type];
    return editOpt;
  } else {
    alert(`未能匹配类型 ${type} ，检查是否写错类型名称`);
  }
};

export const childEditTypeTransform = function(optArr) {
  let children = [];
  optArr.forEach(item => {
    if (item.children && item.children.length > 0) {
      let { data: field, title } = item;
      children.push({
        field,
        title,
        children: childEditTypeTransform(item.children)
      });
    } else {
      let { data: field, filterable, editable, cellType, others } = item;
      let filterAbout = {};
      let editAbout = {};
      let cellAbout = {};
      others = others ? others : {};
      // 筛选
      if (filterable) {
        let { filterSource: filters, filterType: name, filterData } = item;
        if (filters) {
          filterAbout["filters"] = filters;
        }
        if (name) {
          let filterRender = { name };
          if (filterData) {
            filterRender = { name, ...filterData };
          }
          filterAbout["filterRender"] = filterRender;
        }
      }
      // 编辑
      if (editable) {
        let { editType, dataSource } = item;
        dataSource = dataSource ? dataSource : {};
        if (editType) {
          let editRender = editTypeTransform(editType);
          Object.assign(editRender, dataSource);
          editAbout["editRender"] = editRender;
        }
      }
      // 单元格
      if (cellType) {
        let { cellData } = item;
        cellData = cellData ? cellData : {};
        cellAbout["cellRender"] = { name: cellType, ...cellData };
      }

      Object.defineProperty(item, "data", { enumerable: false });

      Object.defineProperty(item, "filterable", { enumerable: false });
      Object.defineProperty(item, "filterSource", { enumerable: false });
      Object.defineProperty(item, "filterType", { enumerable: false });
      Object.defineProperty(item, "filterData", { enumerable: false });

      Object.defineProperty(item, "editable", { enumerable: false });
      Object.defineProperty(item, "editType", { enumerable: false });
      Object.defineProperty(item, "dataSource", { enumerable: false });

      Object.defineProperty(item, "cellType", { enumerable: false });
      Object.defineProperty(item, "cellData", { enumerable: false });

      Object.defineProperty(item, "others", { enumerable: false });

      children.push({
        field,
        ...filterAbout,
        ...editAbout,
        ...cellAbout,
        ...item,
        ...others
      });
    }
  });
  return children;
};
